package com.czl.module_service.viewmodel.assetUse

import androidx.databinding.ObservableField
import com.blankj.utilcode.util.GsonUtils
import com.czl.base.base.BaseBean
import com.czl.base.base.BaseViewModel
import com.czl.base.base.MyApplication
import com.czl.base.binding.command.BindingAction
import com.czl.base.binding.command.BindingCommand
import com.czl.base.data.DataRepository
import com.czl.base.data.bean.tengyun.ListBean
import com.czl.base.data.bean.tengyun.UsageBean
import com.czl.base.event.SingleLiveEvent
import com.czl.base.extension.ApiSubscriberHelper
import com.czl.base.util.RxThreadHelper

class MyUsageApplyViewModel(application: MyApplication, model: DataRepository) :
    BaseViewModel<DataRepository>(application, model) {

    var currentPage = 1
    var pageSize = 10
    val uc = UiChangeEvent()
    var tabIndex : Int? = 1
    var applicationType : Int? = 0
    private var businessId : Int = 2
    var titleName = ObservableField("")

    class UiChangeEvent {
        val loadCompleteEvent: SingleLiveEvent<ListBean<UsageBean>> = SingleLiveEvent()
        val loadFinallyEvent: SingleLiveEvent<Unit?> = SingleLiveEvent()
    }

    val onLoadMoreListener: BindingCommand<Void> = BindingCommand(BindingAction {
        if (tabIndex == 1) {
            this.WXgetMyPurchaseApplicationList(applicationType = applicationType, searchTitle = titleName.get())
        } else {
            businessId = if (applicationType == 0) 2 else 1
            this.getUserApprovalList(businessId = businessId, searchTitle = titleName.get())
        }
    })
    val onRefreshListener: BindingCommand<Void> = BindingCommand(BindingAction {
        currentPage = 1
        if (tabIndex == 1) {
            this.WXgetMyPurchaseApplicationList(applicationType = applicationType, searchTitle = titleName.get())
        } else {
            businessId = if (applicationType == 0) 2 else 1
            this.getUserApprovalList(businessId = businessId, searchTitle = titleName.get())
        }
    })

    val search: BindingCommand<Void> = BindingCommand(BindingAction {
        showLoading()
        currentPage = 1
        if (tabIndex == 1) {
            this.WXgetMyPurchaseApplicationList(applicationType = applicationType, searchTitle = titleName.get())
        } else {
            this.getUserApprovalList(businessId = businessId, searchTitle = titleName.get())
        }
    })

    private fun WXgetMyPurchaseApplicationList(
        companyId: String? = model.getLoginCompanyId(),
        userId: String? = model.getLocalUserInfo()?.userId,
        applicationType: Int? = 0,
        searchTitle: String? = null
    ) {
        model.mTengYunHttpDataSource.WXGetMyApplicationList(
            GsonUtils.toJson(
                mapOf(
                    "pageNo" to currentPage,
                    "pageSize" to pageSize,
                    "userId" to userId,
                    "companyId" to companyId,
                    "titleName" to searchTitle,
                    "applicationType" to applicationType
                )
            )
        )
            .compose(RxThreadHelper.rxSchedulerHelper(this))
            .doFinally {
                uc.loadFinallyEvent.postValue(null)
            }
            .subscribe(object : ApiSubscriberHelper<BaseBean<ListBean<UsageBean>>>() {
                override fun onResult(t: BaseBean<ListBean<UsageBean>>) {
                    if (t.state == 0) {
                        currentPage++
                        uc.loadCompleteEvent.postValue(t.data)
                    }
                    dismissLoading()
                }

                override fun onFailed(msg: String?) {
                    dismissLoading()
                    showErrorToast(msg)
                }

            })
    }

    private fun getUserApprovalList(
        userId: String? = model.getLocalUserInfo()?.userId,
        statuses: ArrayList<Int> = arrayListOf<Int>(1, 2, 3, 4),
        searchTitle: String? = null,
        taskType: Int = 1,   /* 0是抄送，1是审批 */
        businessId: Int = 2
    ) {
        model.mTengYunHttpDataSource.getUserApprovalList(
            GsonUtils.toJson(
                mapOf(
                    "pageNo" to currentPage,
                    "pageSize" to pageSize,
                    "userId" to userId,
                    "statuses" to statuses,
                    "title" to searchTitle,
                    "taskType" to taskType,
                    "businessId" to businessId
                )
            )
        )
            .compose(RxThreadHelper.rxSchedulerHelper(this))
            .doFinally {
                uc.loadFinallyEvent.postValue(null)
            }
            .subscribe(object : ApiSubscriberHelper<BaseBean<ListBean<UsageBean>>>() {
                override fun onResult(t: BaseBean<ListBean<UsageBean>>) {
                    if (t.state == 0) {
                        currentPage++
                        uc.loadCompleteEvent.postValue(t.data)
                    }
                    dismissLoading()
                }

                override fun onFailed(msg: String?) {
                    dismissLoading()
                    showErrorToast(msg)
                }

            })
    }
}